package com.sendpo.zjx.sendpo.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.google.gson.Gson;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.activity.AddressListActivity;
import com.sendpo.zjx.sendpo.activity.AllDindanListActivity;
import com.sendpo.zjx.sendpo.activity.CreateBusStoreActivity;
import com.sendpo.zjx.sendpo.activity.EditZhaoPinActivity;
import com.sendpo.zjx.sendpo.activity.FeedbackActivity;
import com.sendpo.zjx.sendpo.activity.HelpingOrderListActivity;
import com.sendpo.zjx.sendpo.activity.HistoryRecordsActivity;
import com.sendpo.zjx.sendpo.activity.LoginActivity;
import com.sendpo.zjx.sendpo.activity.MainMerchatsMainActivity;
import com.sendpo.zjx.sendpo.activity.MyselfCollectionListActivity;
import com.sendpo.zjx.sendpo.activity.OfficalMessageListActivity;
import com.sendpo.zjx.sendpo.activity.UserMoneyCenterActivity;
import com.sendpo.zjx.sendpo.activity.UserPersonCenterActivity;
import com.sendpo.zjx.sendpo.activity.UserProfileActivity;
import com.sendpo.zjx.sendpo.activity.ZhaoPinListActivity;
import com.sendpo.zjx.sendpo.domain.UserDomain;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.table.User;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by zjx on 2018
 */

public class MyselfMainFragment extends BaseMainFragment {
    @BindView(R.id.index_head)
    RelativeLayout indexHead;
    @BindView(R.id.message_iv)
    ImageView messageIv;
    @BindView(R.id.red_dot)
    View redDot;
    @BindView(R.id.set_iv)
    ImageView setIv;
    @BindView(R.id.circleuser)
    CircleImageView circleuser;
    @BindView(R.id.myself_uname)
    TextView myselfUname;
    @BindView(R.id.myself_acmanager)
    TextView myselfAcmanager;
    @BindView(R.id.myself_moneymanager)
    TextView myselfMoneymanager;
    @BindView(R.id.daishoukuan_iv)
    ImageView daishoukuanIv;
    @BindView(R.id.daifukuan_bt)
    RelativeLayout daifukuanBt;
    @BindView(R.id.daishoustore_iv)
    ImageView daishoustoreIv;
    @BindView(R.id.daishouhuo_bt)
    RelativeLayout daishouhuoBt;
    @BindView(R.id.daiqu_iv)
    ImageView daiquIv;
    @BindView(R.id.daiqubt)
    RelativeLayout daiqubt;
    @BindView(R.id.daifukuan_iv)
    ImageView daifukuanIv;
    @BindView(R.id.daipingjia)
    RelativeLayout daipingjia;
    @BindView(R.id.myselfdingdan_iv)
    ImageView myselfdingdanIv;
    @BindView(R.id.alldindan)
    RelativeLayout alldindan;
    @BindView(R.id.allpinjia_bt)
    RelativeLayout allpinjiaBt;

    @BindView(R.id.dizhimanager_iv)
    ImageView dizhimanagerIv;
    @BindView(R.id.myselfaddressmg_bt)
    RelativeLayout myselfaddressmgBt;
    @BindView(R.id.myhelp_iv)
    ImageView myhelpIv;
    @BindView(R.id.myself_helphelp)
    RelativeLayout myselfHelphelp;
    @BindView(R.id.mybangbang_iv)
    ImageView mybangbangIv;
    @BindView(R.id.editzhaopin_bt)
    RelativeLayout editzhaopinBt;
    @BindView(R.id.myzhibo_iv)
    ImageView myzhiboIv;
    @BindView(R.id.webonline_bt)
    RelativeLayout webonlineBt;
    @BindView(R.id.zhaopingqiuzhi_iv)
    ImageView zhaopingqiuzhiIv;
    @BindView(R.id.zhaopin_bt)
    RelativeLayout zhaopinBt;
    @BindView(R.id.myzuji_iv)
    ImageView myzujiIv;
    @BindView(R.id.myself_zuji)
    RelativeLayout myselfZuji;
    @BindView(R.id.mypinjia_iv)
    ImageView mypinjiaIv;
    @BindView(R.id.mypinjia_bt)
    RelativeLayout mypinjiaBt;
    @BindView(R.id.myytoushu_iv)
    ImageView myytoushuIv;
    @BindView(R.id.myself_tousubt)
    RelativeLayout myselfTousubt;
    @BindView(R.id.mycollection_iv)
    ImageView mycollectionIv;
    @BindView(R.id.myselfcollection_bt)
    RelativeLayout myselfcollectionBt;
    private View rootView;


    public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle) {
        if (this.rootView == null) {
            this.rootView = super.onCreateView(paramLayoutInflater, paramViewGroup, paramBundle);
            ButterKnife.bind(this, this.rootView);
            initData();
        }
        return this.rootView;
    }

    public View initView() {
        View localView = View.inflate(this.mActivity, R.layout.fragment_myself, null);
        return localView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }
    public void initData(){

    }


    public void onStart()
    {
        super.onStart();
        locationUserServer();
        webUserSserver();
        querynoreadyofficalmessagecount();
    }

    public  void webUserSserver(){
               final List localList = Xutils.queryAll(this.mActivity.getApplication(), User.class);
            if ((localList == null) || (localList.size() == 0)) {
                return;
            }
                     final User localUser = (User)localList.get(0);
                        Map<String,String> map=new HashMap<>();
                      map.put("userId", localUser.getUserId() + "");
                  Xutils.POST(HttpUrl.queryuserbyid, map, new Callback.CommonCallback<String>() {
                      @Override
                      public void onSuccess(String result) {
                          System.out.println("**********************rwebUserSserveresult:"+result);
                          Gson gson=new Gson();
                          UserDomain domain=gson.fromJson(result, UserDomain.class);
                          switch (domain.getState()){
                              case HttpCode.SUCCESS:
                                     User   data = domain.getData();
                                  data.setId(localUser.getId());
                                  Xutils.update(MyselfMainFragment.this.mActivity.getApplication(),data);
                                  locationUserServer();
                                  System.out.println("*********************************data:"+data.toString());
                                  break;
                        }
                      }

                      @Override
                      public void onError(Throwable ex, boolean isOnCallback) {

                      }

                      @Override
                      public void onCancelled(CancelledException cex) {

                      }

                      @Override
                      public void onFinished() {

                      }
                  });
        }

    public void querynoreadyofficalmessagecount()
    {
        List<User> localList = Xutils.queryAll(this.mActivity.getApplication(), User.class);
        if ((localList == null) || (localList.size() == 0)) {
            return;
        }
        Map<String,String> map=new HashMap<>();
        map.put("userId",""+localList.get(0).getUserId());
        Xutils.POST(HttpUrl.officalmessagenoreadycount, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject=new JSONObject(result);
                    switch (jsonObject.getInt("state")){
                        case HttpCode.SUCCESS:
                            MyselfMainFragment.this.redDot.setVisibility(View.GONE);
                            break;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    public void locationUserServer()
    {
        List<User> userList = Xutils.queryAll(this.mActivity.getApplication(), User.class);
       if(userList==null||userList.size()==0){
           return;
       }
        User user=userList.get(0);
       if(!TextUtils.isEmpty(user.getUserAvatarpath())){
           Glide.with(this).load(user.getUserAvatarpath()).asBitmap().animate(R.anim.fade_in).centerCrop().error(R.drawable.nim_avatar_default)
            .override(300,300)
                   .into(new SimpleTarget<Bitmap>() {
                       @Override
                       public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                           circleuser.setImageBitmap(resource);
                       }

                   });
       }else{
           circleuser.setImageResource(R.drawable.nim_avatar_default);
       }
        myselfUname.setText(user.getUserName());
    }

   @OnClick(value = {R.id.set_iv,R.id.circleuser,R.id.message_iv,R.id.myselfaddressmg_bt,R.id.myself_helphelp,
   R.id.editzhaopin_bt,R.id.myself_acmanager,R.id.myself_moneymanager,
   R.id.daifukuan_bt,
   R.id.daishouhuo_bt,R.id.daiqubt,R.id.daipingjia,R.id.allpinjia_bt,R.id.zhaopin_bt,R.id.myself_tousubt,
   R.id.myselfcollection_bt,R.id.myself_zuji,R.id.woyaokaidian_bt})
    public void onMyClick(View view){
       Map  map = new HashMap();
       List<User> userList=Xutils.queryAll(getActivity().getApplication(),User.class);
       if(userList==null||userList.size()==0){
          Tools.redirectActivity(mActivity, LoginActivity.class);
           return;
       }
         switch (view.getId()){
             case R.id.woyaokaidian_bt:
                 Tools.redirectActivity(mActivity, MainMerchatsMainActivity.class);
                 break;
           case R.id.myself_zuji:
               Tools.redirectActivity(mActivity, HistoryRecordsActivity.class);
             break;
             case R.id.zhaopin_bt:
                 Tools.redirectActivity(mActivity, ZhaoPinListActivity.class);
                 break;
             case R.id.allpinjia_bt:
                 map.put("position",0);
                 Tools.redirectActivity(mActivity, AllDindanListActivity.class,map);
                 break;
             case R.id.daifukuan_bt:
                 map.put("position",1);
           Tools.redirectActivity(mActivity, AllDindanListActivity.class,map);
           break;

             case R.id.daishouhuo_bt:
                 map.put("position",2);
                 Tools.redirectActivity(mActivity, AllDindanListActivity.class,map);
                 break;

             case R.id.daiqubt:
                 map.put("position",3);
                 Tools.redirectActivity(mActivity, AllDindanListActivity.class,map);
                 break;
             case R.id.daipingjia:
                 map.put("position",4);
                 Tools.redirectActivity(mActivity, AllDindanListActivity.class,map);
                 break;

             case R.id.myself_acmanager:
                 Tools.redirectActivity(this.mActivity, UserProfileActivity.class);
                 break;
             case R.id.set_iv:
                  mActivity.completeExitLogin();
                 break;

             case R.id.circleuser:
                 map.put("id", userList.get(0).getUserId());
                 Tools.redirectActivity(this.mActivity, UserPersonCenterActivity.class, map);
                 break;
             case R.id.message_iv:
                 Tools.redirectActivity(mActivity, OfficalMessageListActivity.class);
                 break;
             case R.id.myselfaddressmg_bt:

                 Tools.redirectActivity(this.mActivity, AddressListActivity.class);
                 break;

             case R.id.myself_helphelp:
                 Tools.redirectActivity(this.mActivity, HelpingOrderListActivity.class);
                 break;
             case R.id.myself_moneymanager:
                 Tools.redirectActivity(this.mActivity, UserMoneyCenterActivity.class);
                 break;
             case R.id.editzhaopin_bt:
                switch (userList.get(0).getMerchantsState()){
                    case 0:
                        Tools.redirectActivity(this.mActivity, CreateBusStoreActivity.class);
                        break;
                    case 1:
                        Toast.makeText(this.mActivity.getApplicationContext(), "店铺实名审核中", Toast.LENGTH_SHORT).show();
                        break;

                    case 2:
               Tools.redirectActivity(this.mActivity, EditZhaoPinActivity.class);

                        break;
                    case 3:
                        Toast.makeText(this.mActivity.getApplicationContext(), "店铺审核实名失败", Toast.LENGTH_SHORT).show();
                        Tools.redirectActivity(this.mActivity, CreateBusStoreActivity.class);
                        break;
                }
                 break;
             case R.id.myself_tousubt:
                 Tools.redirectActivity(this.mActivity, FeedbackActivity.class);
                 break;
             case R.id.myselfcollection_bt:
                 Tools.redirectActivity(this.mActivity, MyselfCollectionListActivity.class);
                 break;
         }
   }
}
